<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
	<div class="wrapper wrapper-content "style="padding: 0 20px;">
		<div class="row">
			<div class="col-sm-12">
				<div class="columns  col-md-2 nopadding" style="margin-bottom: 5px;">
					<input id="searchName" type="text" class="form-control" placeholder="姓名搜索">
				</div>
				<div class="ibox-content" style="overflow: auto;height: 305px;">
					<div id="jsTree"></div>
				</div>
				<div class="form-group " style="margin-top: 15px;">
					<div class="col-sm-12 col-sm-offset-12">
						<button type="submit" onclick="loadUser()" class="btn btn-primary">提交</button>
					</div>
				</div>
			</div>
		</div>
	</div>
	<div th:include="include :: footer"></div>
	<script type="text/javascript">
		$(document).ready(function() {
			getTreeData()
			
			var to = false;
		  $('#searchName').keyup(function () {
		    if(to) { clearTimeout(to); }
		    to = setTimeout(function () {
		      var v = $('#searchName').val();
		      $('#jsTree').jstree(true).search(v);
		    }, 250);
		  });

		});
		function getTreeData() {
			console.log(1);
			$.ajax({
				type : "GET",
				url : "/sys/user/tree",
				success : function(tree) {
					loadTree(tree);
				}
			});
		}
		function loadTree(tree) {
			$('#jsTree').jstree({
				'core' : {
					"multiple": false,//单选
					'data' : tree
				},
				"plugins" : [ "checkbox","search" ],
				"checkbox": {
			        "three_state": false
			      }
			}).on("load_node.jstree",function(e,d){
	            var nodes=d.node.children_d;
	            for(var i in nodes){
	                var node=d.instance.get_node(nodes[i]);
	                if(node.state.mType=="user"){
	                    var _node=node.original;
	                    $('#jsTree').jstree().set_icon(node,"fa fa-user");
	                }else if(node.state.mType=="dept"){
	                    var _node=node.original;
	                    $('#jsTree').jstree().set_icon(node,"fa fa-newspaper-o");
	                }else{
	                	var _node=node.original;
	                    $('#jsTree').jstree().set_icon(node,"fa fa-home");
	                }
	            }
	        });
 
			$('#jsTree').jstree().open_all();
		}
		function loadUser(){
			var userNames,userIds;
			var ref = $('#jsTree').jstree(true); // 获得整个树
			userIds = ref.get_bottom_selected();
			users = ref.get_bottom_checked('true');
			if(userIds.length<=0||users[0].state.mType!="user"){
				layer.msg("请选择人员");
				return;
			}
			var txt="";
			for(var user in users){
				if(users[user].state.mType=="user"){
					//txt=txt+users[user].text+",";
					txt=txt+users[user].text;
				}
			}
			for(var i=0;i<userIds.length;i++){
				userIds[i]=userIds[i].replace("u_","");
			}
			parent.loadUser(userIds,txt);
			var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
			parent.layer.close(index);
		}
	</script>
</body>
</html>
